from flask import Blueprint, render_template, session, redirect, url_for
from core.post.form import PostForm
from models import Post, to_binary, insert

post_blueprint= Blueprint('post', __name__)


@post_blueprint.route('/post',methods=['GET','POST'])
def post():
    form = PostForm()
    if form.validate_on_submit():
        p = Post()
        if form.img.data:
            p.img = to_binary(form.img.data)
        p.sort = form.sort.data
        p.text = form.text.data
        p.uid = session['uid']
        insert(p)
        return redirect(url_for('post.post'))

    return render_template('post/index.html',form=form)